package io.renren.modules.password.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.renren.common.constant.Constant;
import io.renren.common.page.PageData;
import io.renren.common.service.impl.BaseServiceImpl;
import io.renren.common.utils.ConvertUtils;
import io.renren.common.utils.Result;
import io.renren.modules.password.dao.PasswordInfoDao;
import io.renren.modules.password.dto.PasswordInfoDTO;
import io.renren.modules.password.entity.PasswordInfoEntity;
import io.renren.modules.password.service.PasswordInfoService;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Author huan.xu
 * @Date 2022/11/2 11:21
 * @Description 密码信息service接口实现
 */
@Service
public class PasswordInfoServiceImpl extends
    BaseServiceImpl<PasswordInfoDao, PasswordInfoEntity> implements
    PasswordInfoService {

  @Override
  public PageData<PasswordInfoDTO> page(Map<String, Object> params) {
    //转换成like
    paramsToLike(params, "title");
    paramsToLike(params, "account");

    //分页
    IPage<PasswordInfoEntity> page = getPage(params, Constant.CREATE_DATE, false);

    //查询
    List<PasswordInfoDTO> list = baseDao.getInfoList(params);
    return getPageData(list, page.getTotal(), PasswordInfoDTO.class);
  }

  /**
   * 保存密码信息
   */
  @Override
  @Transactional(rollbackFor = Exception.class)
  public Result<String> save(PasswordInfoDTO passwordInfoDTO) {
    PasswordInfoEntity entity = ConvertUtils
        .sourceToTarget(passwordInfoDTO, PasswordInfoEntity.class);
    baseDao.insert(entity);
    return new Result<>();
  }


  /**
   * 修改密码
   */
  @Override
  @Transactional(rollbackFor = Exception.class)
  public Result<String> update(PasswordInfoDTO dto) {
    PasswordInfoEntity passwordInfoEntity = ConvertUtils
        .sourceToTarget(dto, PasswordInfoEntity.class);
    baseDao.updateById(passwordInfoEntity);
    return new Result<>();
  }

  /**
   * 删除密码信息
   */
  @Override
  public void delete(Long[] ids) {
    baseDao.deleteBatchIds(Arrays.asList(ids));
  }

  /**
   * 根据Id查询
   */
  @Override
  public PasswordInfoDTO getById(Long id) {
    PasswordInfoEntity passwordInfoEntity = baseDao.selectById(id);
    return ConvertUtils.sourceToTarget(passwordInfoEntity, PasswordInfoDTO.class);
  }
}
